﻿using Client.Todo.Common;
using Common.Lib.Dto;
using Common.Lib.Service;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Client.Todo.ViewModels.Dialogs
{
    public class AddToDoViewModel : ViewModelBase, IDialogHostAware
    {
        private readonly IToDoService toDoService;

        /// <summary>
        /// 添加待办
        /// </summary>
        public AddToDoViewModel(IContainerProvider containerProvider, IToDoService toDoService)
        {
            OkCmd = new DelegateCommand(ExceuteOk);
            CancelCmd = new DelegateCommand(ExceuteCancel);
            IgnoreCmd = new DelegateCommand(ExceuteIgnore);
            this.toDoService = toDoService;
            
        }

        async void ExceuteOk()
        {
            if (string.IsNullOrEmpty(EditData.Title))
            {
                return;
            }
            if (string.IsNullOrEmpty(EditData.Content))
            {
                return;
            }
            if (EditData?.Id > 0)
            {
                var res = await toDoService.UpdateAsync(EditData);
                if (res?.Status == true)
                {
                    DialogParameters parameter = new DialogParameters();
                    parameter.Add("Value", EditData);
                    //parameter.Add()
                    DialogHost.Close(DialogHostNmae, new DialogResult(ButtonResult.OK, parameter));
                }
            }
            else
            {
                var res = await toDoService.AddAsync(EditData);
                if (res?.Status == true)
                {
                    DialogParameters parameter = new DialogParameters();
                    parameter.Add("Value", EditData);
                    //parameter.Add()
                    DialogHost.Close(DialogHostNmae, new DialogResult(ButtonResult.OK, parameter));
                }
            }
        }
        void ExceuteCancel()
        {
            DialogHost.Close(DialogHostNmae, new DialogResult(ButtonResult.Cancel, new DialogParameters()));
        }
        void ExceuteIgnore()
        {

        }


        #region 当前编辑的Todo

        /// <summary>
        /// 当前编辑的Todo
        /// </summary>
        private ToDoDto _editData;

        /// <summary>
        /// 当前编辑的Todo
        /// </summary>
        public ToDoDto EditData
        {
            get { return _editData; }
            set { SetProperty(ref _editData, value); }
        }

        #endregion 当前编辑的Todo 

        public string Title { get; set; }
        public string DialogHostNmae { get; set; }
        public DelegateCommand OkCmd { get; set; }
        public DelegateCommand IgnoreCmd { get; set; }
        public DelegateCommand CancelCmd { get; set; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }


        public void OnDialogOpened(IDialogParameters parameters)
        {
            if(parameters?.ContainsKey("Value") == true)
            {
                EditData = parameters.GetValue<ToDoDto>("Value");
            }
            else
            {
                EditData = new ToDoDto();
            }
        }
    }
}
